<?php

namespace serv\queue\warehouse;

use common\batchInsert;
use of;
use L;
use traits\StockTrait;

class stock
{
    public static function stockQueue($arr)
    {
        try {
            set_time_limit(0);
            $data = $arr['data'];
            if (1 == $data['order_type']) {
                //头程发货
            } else {
                //尾程发货
            }
            return true;
        } catch (Exception $exp) {
            return false;
        }
    }

    //统计在库，在途， 采购中三段库存的总数量
    public static function stockSummaryQueue($arr)
    {
        try {
            set_time_limit(0);
            $data = $arr['data'];
            if (!empty($data)) {
                foreach ($data as $v) {
                    $sql = "SELECT id FROM stock_summary WHERE 1 AND sku = '{$v['sku']}';";
                    $result = L::sql($sql, 'warehouse');
                    if (!empty($result)) {
                        //更新数据
                        $id = $result[0]['id'];
                        if (isset($v['stock'])) {
                            //统计的是在库库存，则更新在库库存
                            $sql = "UPDATE stock_summary SET stock = '{$v['stock']}', 
                                    available_stock = '{$v['available_stock']}', 
                                    lock_stock = '{$v['lock_stock']}'
                                    WHERE 1 AND id = '{$id}';";
                        } else if (isset($v['purchase_total'])) {
                            $sql = "UPDATE stock_summary SET on_produce = '{$v['purchase_total']}' WHERE 1 AND id = '{$id}';";
                        } else {
                            $sql = "UPDATE stock_summary SET on_way_stock = '{$v['shipped_total']}' WHERE 1 AND id = '{$id}';";
                        }
                        L::sql($sql, 'warehouse');
                    } else {
                        //新增数据
                        $param = [
                            'sku' => $v['sku'],
                            'sku_name' => $v['sku_name'],
                            'created_by' => of::config('env.SYSTEM_ID'),
                            'created_at' => date('Y-m-d H:i:s'),
                            'updated_by' => of::config('env.SYSTEM_ID'),
                            'updated_at' => date('Y-m-d H:i:s'),
                        ];
                        if (isset($v['stock'])) {
                            //统计的是在库库存，则更新在库库存
                            $param = array_merge($param, [
                                'stock' => $v['stock'],
                                'available_stock' => $v['available_stock'],
                                'lock_stock' => $v['lock_stock']
                            ]);
                        } else if (isset($v['purchase_total'])) {
                            $param = array_merge($param, [
                                'on_produce' => $v['purchase_total']
                            ]);
                        } else {
                            $param = array_merge($param, [
                                'on_way_stock' => $v['shipped_total']
                            ]);
                        }

                        $sql = batchInsert::insert('stock_summary', [$param]);
                        L::sql($sql, 'warehouse');
                    }
                }
            }
            return true;
        } catch (Exception $exp) {
            return false;
        }
    }



}
